/**
 *
 */
package org.insia.smartposse.manager.list;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.insia.smartposse.form.SearchFreelancesForm;
import org.insia.smartposse.manager.SkillManager;
import org.insia.smartposse.manager.UserManager;
import org.insia.smartposse.mapping.Users;
import org.insia.smartposse.mapping.dao.UsersDAO;

/**
 * Manage the Freelance List
 *
 * @author cmougel
 *
 */
public class FreelancesListManager extends ArrayList {

	private List<Users> _freelances = new ArrayList<Users>();
	private final UsersDAO _dao = new UsersDAO();
	private final Session _session;
	private String _name = "";
	private boolean _search = false;
	private SkillManager _skill = null;


	/**
	 * Get All Project
	 *
	 */
	public FreelancesListManager(){
		_session = _dao.getSession();
//		_freelances = _dao.findAllFreelance(_session);
	}

	public FreelancesListManager(SearchFreelancesForm freelancesForm) {
		this();
		setName(freelancesForm.getName());
		if (0 < freelancesForm.getSkill()){
			setSkill(new SkillManager(freelancesForm.getSkill()));
		}
	}

	private void setSkill(SkillManager skill) {
		_skill = skill;
		_search = false;
	}

	private void setName(String name) {
		_name = name;
		_search = false;

	}

	private void search(){
		if (!_search){
			_freelances = _dao.search(_session, UsersDAO.FREELANCE, _name, _skill);
		}
	}

	/**
	 * Destruct and close the session
	 */
	public void finalize(){
		//UsersDAO.closeCurrentSession();
	}

	@Override
	public UserManager get(int index) {
		search();
		return new UserManager(_freelances.get(index));
	}

	@Override
	public int size() {
		search();
		return _freelances.size();
	}
}
